package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start(){
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请选择功能：");
//        System.out.println("1.用户注册 ");
//        System.out.println("2.用户登录 ");
//        System.out.println("3.用户列表 ");
//        userMapper.insert(null);
//        System.out.println("UserController:注册成功！");

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入数字编号：");
        System.out.println("1.用户注册");
        System.out.println("2.用户登录");
        System.out.println("3.查询用户列表");
        System.out.println("4.修改用户信息");
        int num=scanner.nextInt();

        switch(num){
            case 1:
                userRegister();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
                break;
            default:
                System.out.println("输入错误！");
                break;
        }
    }




    private void userRegister(){
        System.out.println("开始注册");
        User user=new User();
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(scanner.next());
        System.out.println("请输入密码：");
        user.setPassword(scanner.next());
        System.out.println("请输入昵称：");
        user.setNickname(scanner.next());
        System.out.println("请输入年龄：");
        user.setAge(scanner.nextInt());
        System.out.println(user);
//        int result = userMapper.insert(user);
//        System.out.println(result>0?"注册成功":"注册失败");

        if(userMapper.selectByUsername(user.getUsername())==null){
            int result = userMapper.insert(user);
            System.out.println(result>0?"注册成功":"注册失败");
        }else{
            System.out.println("用户【"+user.getUsername()+"】已存在");
        }



    }

    private void userLogin(){
        System.out.println("开始登录");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=scanner.nextLine();
        System.out.println("请输入密码：");
        String password=scanner.nextLine();

        User user=userMapper.selectByUsername(username);
        if(user!=null) {
            if (user.getPassword().equals(password)) {
                System.out.println("登录成功！欢迎【" + user.getNickname() + "】回来！");

            } else {
                System.out.println("登录失败！用户名或密码错误！");
            }
        }else{
            System.out.println("登录失败！用户不存在！");
        }
    }

    private void userList(){
        System.out.println("展示用户列表");
        List<User> userList = userMapper.selectAll();
        for (User user : userList) {
            System.out.println(user);
        }
    }

    private void userUpdate(){
        System.out.println("修改用户信息");
        User user=new User();
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=scanner.nextLine();
        System.out.println("请输入新的密码：");
        String password=scanner.nextLine();
        System.out.println("请输入新的昵称：");
        String nickname=scanner.nextLine();
        System.out.println("请输入新的年龄：");
        user.setAge(scanner.nextInt());
        int result=userMapper.update(user);
        System.out.println(result>0?"修改成功！":"修改失败！");
    }
}
